package com.itcs.box.server.resolver;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.itcs.box.BoxConstants;
import com.itcs.box.domain.BoxData;
import com.itcs.box.domain.BoxResponse;
import com.itcs.box.server.server.NettyChannelMap;

public class SendSocket2 {
	private static final Logger LOG = Logger.getLogger(SendSocket2.class);//下发日志
	/**
	 * 支付成功通知应答
	 * @param connectId
	 * @param deviceId
	 * @param key
	 */
	public static void sendZfcgtz(String deviceId,  String transacSeq, String payChannelCode) {
		String messgeType = "2008";
		String key = "2";
		try {
			Long iTime = System.currentTimeMillis();
			BoxData responseData2008 = new BoxData();
			responseData2008.setDeviceId(deviceId);
			responseData2008.setTransacSeq(transacSeq);
			responseData2008.setPayType("4");
			responseData2008.setPayChannelCode(payChannelCode);
			responseData2008.setPayResult("1");
			responseData2008.setInvoice("1");
			responseData2008.setRequestTime(String.valueOf(iTime));
			writeChannelMessage(responseData2008,null,messgeType,key);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 出口白名单下发 
	 * @param connectId
	 * @param plateTotal
	 * @param carPlate
	 * @param plateColor
	 * @param carClass
	 * @param requestTime
	 * @param transacSeq
	 * @param key
	 */
	public static void sendExitWhitelist(String deviceId, String plateNumStr, String carPlateStr, String plateColorStr,String carClassStr) { 
		String messgeType = "2005";
		String key = "2";
		String iTime = String.valueOf(System.currentTimeMillis());
		String requestTime = iTime;
		try {
			BoxData rData = new BoxData();
			rData.setPlateNum(plateNumStr);
			rData.setiCarPlate(carPlateStr);
			rData.setiPlateColor(plateColorStr);
			rData.setiCarClass(carClassStr);
			rData.setiTollStationCode("");
			rData.setiLaneNo("");
			rData.setiTime(iTime);
			rData.setIsCard("2");
			rData.setDeviceId(deviceId);
			rData.setRequestTime(requestTime);
			writeChannelMessage(rData,null,messgeType,key);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * 与通讯盒之间的通信
	 * @param e
	 * @param responseData
	 * @param data
	 * @param messgeType
	 * @param result
	 */
	public static void writeChannelMessage(BoxData responseData, BoxData data,String messgeType,String result){
		writeChannelMessage(responseData,data,messgeType,result,null);
	}

	/**
	 * 与通讯盒之间的通信
	 * @param e
	 * @param responseData
	 * @param data
	 * @param messgeType
	 * @param result
	 */
	public static void writeChannelMessage(BoxData responseData, BoxData data,String messgeType,String result,String messResult){
		// 创建解析json格式对象
		String toJsonString = "";
		
		BoxResponse responseInfo = new BoxResponse();
		
		String messageLog = "";
		
		
		//=========================================返回===========================================
		
		if(responseData==null){
			responseData = new BoxData();
		}
		// 返回应答信息
		responseInfo.setMessgeType(messgeType);
		if(data!=null){
			//设备ID
			responseData.setDeviceId(data.getDeviceId());
			//生成交易序列
			responseData.setTransacSeq(data.getTransacSeq());// 回复黑盒信息
		}
		responseData.setResult(result);// 接收结果：0：成功，-1，解析失败，-2：其他原因
		responseData.setMessResult(messResult);//返回失败说明
		responseData.setRequestTime(String.valueOf(System.currentTimeMillis()));
		
		responseInfo.setData(responseData);
		// 转换成json格式，
		ByteBuf byteBuf = Unpooled.wrappedBuffer(JSON.toJSONString(responseInfo).getBytes(BoxConstants.CHARSET_UTF8));
		
		String deviceId = responseData.getDeviceId();
		
		ChannelHandlerContext chc = NettyChannelMap.get(deviceId);
		if(chc!=null){
			chc.writeAndFlush(byteBuf);
		}else{
			LOG.error("writeChannelMessage_deviceId为空"+deviceId);
		}
	}
}
